
package com.study.services;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.study.model.mdao.LogforcustDAO;
import com.study.model.mdao.OpportunityMapper;
import com.study.model.mdao.UserMDAO;


@Service
@Transactional(rollbackOn = Exception.class)
public class OpportunityService {
	@Autowired
	OpportunityMapper mapper;
	@Autowired
	LogforcustDAO logforcustDAO;
	@Autowired
	UserMDAO udao;
	/**
	 * 带条件的分页查询
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @param name
	 * @param selects
	 * @param cusid 
	 * @return
	 */
	public List<JSONObject> selectBySel(int pageNum, int pageSize, String name, String selects,String  useid, Integer cusid) {
		String state = null;
		String priority = null;
		PageHelper.startPage(pageNum, pageSize);
		if (selects.length() > 0 && selects.startsWith("311")) {
			state = selects;
		} else if (selects.length() > 0 && selects.startsWith("313")) {
			priority = selects;
			System.out.println("进来" + state);
		}
		String input = "%" + name + "%";
		List<JSONObject> selectwhere = new ArrayList<JSONObject>();
		if ("6".equals(useid)) {
        	selectwhere=mapper.selectByWhere(state, priority, input,cusid);
		}else {
			selectwhere=mapper.selectByWhereUse(state, priority, input,useid,cusid);
		}
		return selectwhere;
	}
	/**
	 * 添加新机会
	 * @param theme
	 * @param custname
	 * @param state
	 * @param type
	 * @param linkman
	 * @param laiyuan
	 * @param users
	 * @param demand
	 * @param possbity
	 * @param exprice
	 * @param youxian
	 * @param stage
	 * @param exdate
	 * @return
	 */
	public boolean insertopp(String theme, String custid, String state, String type, String linkman, String laiyuan,
			String useid, String demand, String possbity, String exprice, String youxian, String stage,
			Date exdate,Date date) {
		 String name = udao.getfindUsreid(useid).getString("use_name");
		   boolean flag = false;
		   int state1=Integer.parseInt(state);
		   int type1=Integer.parseInt(type);
		   int laiyuan1 =  Integer.parseInt(laiyuan);
		   int youxian1 = Integer.parseInt(youxian);
		   int stage1 = Integer.parseInt(stage);
		   double exprices = 0.0;
		   if (exprice!=null&&exprice!="") {
			    exprices = Double.parseDouble(exprice);
		   }
			  flag = mapper.insertopp( theme,  custid,  state1,  type1,  102,  laiyuan1, 
					  useid,  demand,  possbity,  exprices,  youxian1,  stage1, exdate,date,date);
		if (flag) {
			 String logMessage ="员工"+name+"在"+new Date()+"添加了一条销售机会信息";
				logforcustDAO.insert(Integer.parseInt(custid), new Date(), Integer.parseInt(useid), 2202, logMessage, "");
		}
		return flag;
	}
	
	public JSONObject selectByid(String id) {
		return mapper.selectByid(Integer.parseInt(id));
	}
	
	
	public boolean updateopp(String oppid,String theme, String custname, String state, String type, String linkman, String laiyuan,
			 String demand, String possbity, String exprice, String youxian, String stage,
			Date exdate,String useid,String custid,String linid) {
		 String name = udao.getfindUsreid(useid).getString("use_name");
		   boolean flag = false;
		   int state1=Integer.parseInt(state);
		   int type1=Integer.parseInt(type);
		   int laiyuan1 =  Integer.parseInt(laiyuan);
		   int youxian1 = Integer.parseInt(youxian);
		   int stage1 = Integer.parseInt(stage);
		   double exprices = 0.0;
		   if (exprice!="") {
			    exprices = Double.parseDouble(exprice);
		}
			  flag = mapper.updateopport(Integer.parseInt(oppid), theme,  Integer.parseInt(custid),  state1,  type1,   Integer.parseInt(linid),  laiyuan1, 
					 6,  demand,  possbity,  exprices,  youxian1, stage1, exdate,new Date());
			  String logMessage ="员工"+name+"在"+new Date()+"修改了  " +theme + "销售机会信息";
				logforcustDAO.insert(Integer.parseInt(custid), new Date(), Integer.parseInt(useid), 222, logMessage, "");
		return flag;
	}
	
	/**
	 * 销售机会视图
	 * @param id
	 * @return
	 */
	public List<JSONObject> selectView(String id) {
		List<JSONObject> js= new ArrayList<>();
		List<JSONObject>  list = mapper.selectView(id);
		//得到最新的一条记
			js.add(list.get(list.size()-1));	
		return js;
	}
	/**
	 * 查找某一销售机会的各个阶段数量
	 * @param id
	 * @return
	 */
	public JSONObject selectCount(String id) {
		
		return mapper.selectCount(id);
	}

	
}

